package com.baizhi.cmfz.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baizhi.cmfz.annotation.LogAnnotation;
import com.baizhi.cmfz.entiy.Guru;
import com.baizhi.cmfz.service.GuruService;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("guru")
public class GuruController {

    @Autowired
    private GuruService service;
    private Logger logger= LoggerFactory.getLogger(GuruController.class);


    //分页和搜索
    @RequestMapping("/getGuruByPage")
    @ResponseBody
    @LogAnnotation("查看或搜索上师")
    public Map getGuruByPage(int page, int rows, String guruName){

        Map map = service.getGuruByPage(page,rows,guruName);
        //System.out.println(map.get("rows"));
        return map;
    }
    //添加
    @RequestMapping("add")
    @ResponseBody
    @LogAnnotation("添加上师")
    public Map add(Guru guru, MultipartFile image, HttpServletRequest request) throws IOException {

        HashMap map = new HashMap();
        System.out.println("guru"+guru);
        //String realPath = request.getRealPath("upload");//已过时的方法：

        //System.out.println("-------------"+image.getOriginalFilename());
        String newName = new Date().getTime() + image.getOriginalFilename();
        System.out.println("newName"+newName);
        File file=new File("C:/Users/吕厚厚/IdeaProjects/cmfz/src/main/webapp/img/"+newName);
        image.transferTo(file);//转为磁盘文件
        guru.setGuruImage("/img/"+newName);
        try {
            service.add(guru);
            map.put("isAdd",true);
        } catch (Exception e) {
            map.put("isAdd",false);
            e.printStackTrace();
        }
        return map;
    }
    //修改
    @RequestMapping("update")
    @ResponseBody
    @LogAnnotation("修改上师")
    public Map update(Guru guru,MultipartFile image, HttpServletRequest request) throws IOException {
        HashMap map = new HashMap();
        System.out.println("guru"+guru);
        //String realPath = request.getRealPath("upload");

        System.out.println("-------------"+image.getOriginalFilename());
        String newName = new Date().getTime() +"_"+ image.getOriginalFilename();

        File file=new File("C:/Users/吕厚厚/IdeaProjects/cmfz/src/main/webapp/img/"+newName);
        image.transferTo(file);//转为磁盘文件
        guru.setGuruImage("/img/"+newName);
        try {
            service.update(guru);
            map.put("isUpdate",true);
        } catch (Exception e) {
            map.put("isUpdate",false);
            e.printStackTrace();
        }
        return map;
    }
    //批量删除
    @RequestMapping("multiDelete")
    @ResponseBody
    @LogAnnotation("上师的批量删除")
    public Map multiDelete(int[] ids ){
        HashMap map = new HashMap();
        logger.debug("数组"+ids);
        try {
            service.multiDelete(ids);
            map.put("isDelete",true);
        } catch (Exception e) {
            map.put("isDelete",false);
            e.printStackTrace();
        }
        return map;
    }
    //数据读取下载
    @RequestMapping("download")

    @LogAnnotation("下载上师数据")
    public void  dataUoload( HttpServletRequest request, HttpServletResponse response) throws IOException {

        //  1.获取要导出的数据
            List<Guru> list = service.selectAll();
            System.out.println(list);
            //2.用easypoi的Excel导出工具类导出Excel，四个导出参数，标题，表名，实体类对象，数据集合
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("上师表所有数据", "gurus"), Guru.class, list);
            //3.通过文件对象写输出流到磁盘
            //  write(new FileOutputStream(new File( "E:/file/guru2.xls")));
            workbook.write(response.getOutputStream());
    }


    //Excel文件数据上传
    @RequestMapping("upload")
    @ResponseBody
    @LogAnnotation("上传上师数据")
    public String dateUpload2(@RequestParam MultipartFile file) throws Exception {
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);

        //  MultipartFile类型的file，直接通过file.getInputStream()得到一个InputStream
        List<Guru> list = ExcelImportUtil.importExcel(file.getInputStream(), Guru.class, params);
        for (Guru guru : list) {
            service.add(guru);
        }
        return"ok";
    }
}
